/*
ONEGAME
Copyright (C) 2009  Pablo Caballero

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package one.map;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Point;
import java.awt.image.BufferedImage;
import java.util.HashMap;
import one.language.Labels;

/**
 * Abstract layer
 * @author Pablo Caballero
 * @version 1.0 23-jun-2009
 */
public class DefaultLayerMap implements ILayerMap {

    /**
     * The cells
     */
    private HashMap<Point, Object> cell;
    /**
     * The width of the map
     */
    private int width;
    /**
     * The height of the map
     */
    private int height;
    /**
     * The name of the layer
     */
    private String name;
    /**
     * Visibility property
     */
    private boolean visible;
    /**
     * Rendering buffer
     */
    private Image buffer;

    public DefaultLayerMap() {
        name = Labels.getString("LAYERMAP_EMPTYLAYER");
        cell = new HashMap<Point, Object>();
        setVisible(true);
        setDimension(new Dimension(1, 1));
    }

    public Dimension getDimension() {
        return new Dimension(width, height);
    }

    public void setDimension(Dimension d) {
        if (!d.equals(getDimension())) {
            width = (int) d.getWidth();
            height = (int) d.getHeight();
            adjustCells();
            buffer = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB_PRE);
        }
    }

    public Object get(int x, int y) {
        return cell.get(new Point(x, y));
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return getName();
    }

    public Object getChild(int index) {
        return 0;
    }

    public int getChildCount() {
        return 0;
    }

    public boolean isLeaf() {
        return true;
    }

    /**
     * Adjust the maps (0,0) -> (width-1, height-1)
     */
    private void adjustCells() {
        HashMap<Point, Object> filtered = new HashMap<Point, Object>();
        for (Point point : cell.keySet()) {
            if (point.getX() < width && point.getY() < height) {
                filtered.put(point, cell.get(point));
            }
        }
        cell.clear();
        cell.putAll(filtered);
    }

    public boolean isVisible() {
        return visible;
    }

    public void setVisible(boolean visible) {
        this.visible = visible;
    }

    public Image paint(long utc) {
        Graphics gbuffer;
        Color prevColor;
        Font prevFont;

        gbuffer = buffer.getGraphics();
        prevFont = gbuffer.getFont();
        prevColor = gbuffer.getColor();

        paint(gbuffer);

        gbuffer.setColor(prevColor);
        gbuffer.setFont(prevFont);
        
        return buffer;
    }

    public void paint(Graphics g) {
        g.setColor(Color.red);
        g.drawLine(0, 0, 100, 100);
    }
}
